from PIL import Image
import os

# 打开 BMP 图片
img_folder = "/nfs/home/xh/OQC/tiny_Data/NG_A2C0375270200250214085021N05AX_0217083858_A2/photo"

for img_name in os.listdir(img_folder):
    print(f"图片名: {img_name}")
    if "bmp" not in img_name:
        continue
    img_path = os.path.join(img_folder, img_name)
    img = Image.open(img_path)
    # 获取原始 BMP 文件大小
    original_size = os.path.getsize(os.path.join(img_folder, f"{img_name}")) / 1024  # 转换为 KB
    print(f"原始 BMP 文件大小: {original_size:.2f} KB, {original_size/1024:.2f} MB")

    img_name = img_name[:-4]
    # 转换为 JPEG（有损压缩）
    jpg_path = os.path.join(img_folder, f"{img_name}.jpg")
    img.save(jpg_path, format="JPEG", quality=95)
    jpeg_size = os.path.getsize(jpg_path) / 1024
    print(f"转换为 JPEG 后的文件大小: {jpeg_size:.2f} KB, {jpeg_size/1024:.2f} MB")

    # 转换为 PNG（无损压缩）
    png_path = os.path.join(img_folder, f"{img_name}.png")
    img.save(png_path, format="PNG")
    png_size = os.path.getsize(png_path) / 1024
    print(f"转换为 PNG 后的文件大小: {png_size:.2f} KB, {png_size/1024:.2f} MB")

    # 转换为 WebP（无损模式）
    webp_path = os.path.join(img_folder, f"{img_name}.webp")
    img.save(webp_path, format="WebP", lossless=True)
    webp_size = os.path.getsize(webp_path) / 1024
    print(f"转换为 WebP（无损）后的文件大小: {webp_size:.2f} KB, {webp_size/1024:.2f} MB")